<template>
  <div class="search">
    <Card>
      <Tabs value="LIST" @on-click="paneChange">
        <TabPane label="发送任务列表" name="LIST">
          <Row class="operation" style="margin-bottom: 10px">
            <Button @click="sendBatchSmsModal" type="primary">发送短信</Button>
            <Button @click="statusRecord" type="info">状态记录</Button>
          </Row>
          <Table
            :loading="loading"
            border
            :columns="smsColumns"
            :data="smsData"
            ref="table"
          >
          </Table>

          <Row type="flex" justify="end" class="mt_10">
            <Page
              :current="smsSearchForm.pageNumber"
              :total="smsTotal"
              :page-size="smsSearchForm.pageSize"
              @on-change="smsChangePage"
              @on-page-size-change="smsChangePageSize"
              :page-size-opts="[10, 20, 50, 100]"
              size="small"
              show-total
              show-elevator
              show-sizer
            ></Page>
          </Row>
        </TabPane>
        <TabPane label="短信模板" name="TEMPLATE">
          <Row class="operation" style="margin-bottom: 10px">
            <Button @click="addTemplate" type="primary">添加短信模板</Button>
            <Button @click="syncTemplate" type="info">同步</Button>
          </Row>
          <Table
            :loading="loading"
            border
            :columns="templateColumns"
            :data="templateData"
            ref="table"
          >
          </Table>
          <Row type="flex" justify="end" class="mt_10">
            <Page
              :current="templateSearchForm.pageNumber"
              :total="templateTotal"
              :page-size="templateSearchForm.pageSize"
              @on-change="templateChangePage"
              @on-page-size-change="templateChangePageSize"
              :page-size-opts="[10, 20, 50, 100]"
              size="small"
              show-total
              show-elevator
              show-sizer
            ></Page>
          </Row>
        </TabPane>
        <TabPane label="短信签名" name="SIGN">
          <Row class="operation" style="margin-bottom: 10px">
            <Button @click="addSign" type="primary">添加短信签名</Button>
            <Button @click="syncSign" type="info">同步</Button>
          </Row>
          <Table
            :loading="loading"
            border
            :columns="signColumns"
            :data="signData"
            ref="table"
          >
            <template slot="signStatus" slot-scope="scope">
              <div v-if="scope.row.signStatus == 2">
                审核拒绝
                <Poptip
                  trigger="hover"
                  :content="scope.row.reason"
                  placement="top-start"
                  transfer
                >
                  <span style="color: #ed3f14">【原因】</span>
                </Poptip>
              </div>
              <div v-if="scope.row.signStatus == 0">审核中</div>
              <div v-if="scope.row.signStatus == 1">审核通过</div>
            </template>
          </Table>
          <Row type="flex" justify="end" class="mt_10">
            <Page
              :current="signSearchForm.pageNumber"
              :total="signTotal"
              :page-size="signSearchForm.pageSize"
              @on-change="signChangePage"
              @on-page-size-change="signChangePageSize"
              :page-size-opts="[10, 20, 50, 100]"
              size="small"
              show-total
              show-elevator
              show-sizer
            ></Page>
          </Row>
        </TabPane>
      </Tabs>
    </Card>
    <!-- 状态记录 -->
    <Modal
      :footer-hide="true"
      :title="sendRecordTitle"
      v-model="sendRecordModal"
      :mask-closable="false"
      @on-cancel="sendRecordClose"
      :width="500"
    >
      <Form>
        <FormItem label="发送时间" prop="templateName">
          <DatePicker
            v-model="selectDate"
            type="datetimerange"
            format="yyyy-MM-dd"
            clearable
            @on-change="selectDateRange"
            placeholder="选择起始时间"
            style="width: 300px"
          ></DatePicker>
          <Button
            @click="statusRecordSearch"
            type="primary"
            style="margin-left: 20px"
            >搜索</Button
          >
        </FormItem>
        <div>
          <span style="margin-right: 20px; margin-left: 60px"
            >成功条数：{{ successCount }}条</span
          >
          <span>失败条数：{{ failCount }}条</span>
        </div>
      </Form>
    </Modal>
    <Modal
      :title="templateModalTitle"
      v-model="templateModalVisible"
      :mask-closable="false"
      :width="500"
    >
      <Form
        ref="templateForm"
        :model="templateForm"
        :label-width="100"
        :rules="templateFormValidate"
      >
        <FormItem label="模板名称" prop="templateName">
          <Input
            v-model="templateForm.templateName"
            maxlength="30"
            clearable
            style="width: 90%"
            placeholder="请输入模板名称，不超过30字符"
          />
        </FormItem>
        <FormItem label="模板内容" prop="templateContent">
          <Input
            v-model="templateForm.templateContent"
            clearable
            type="textarea"
            style="width: 90%"
            maxlength="200"
            :autosize="{ maxRows: 5, minRows: 5 }"
            show-word-limit
            placeholder="请输入短信内容，不超过500字符，不支持【】、★、 ※、 →、 ●等特殊符号；"
          />
        </FormItem>
        <FormItem label="申请说明" prop="remark">
          <Input
            v-model="templateForm.remark"
            clearable
            type="textarea"
            style="width: 90%"
            maxlength="150"
            :autosize="{ maxRows: 4, minRows: 4 }"
            show-word-limit
            placeholder="请描述您的业务使用场景，不超过100字符；如：用于春节集五福"
          />
        </FormItem>
      </Form>
      <div slot="footer">
        <Button type="text" @click="templateModalVisible = false">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="templateSubmit"
          >提交
        </Button>
      </div>
    </Modal>
    <Modal
      :title="sendSmsModalTitle"
      v-model="sendSmsModal"
      :mask-closable="false"
      :width="900"
      @on-cancel="close"
    >
      <div class="send-setting">
        <div class="send-form">
          <Form
            ref="smsForm"
            :model="smsForm"
            :label-width="100"
            :rules="smsFormValidate"
          >
            <FormItem label="短信签名" prop="signName">
              <Select
                @on-change="selectSmsSign"
                v-model="smsForm.signName"
                style="width: 35%"
              >
                <Option
                  v-for="item in smsSigns"
                  :value="item.signName"
                  :key="item.id"
                  >{{ item.signName }}
                </Option>
              </Select>
            </FormItem>
            <FormItem label="短信模板" prop="messageCode">
              <Select
                @on-change="selectSmsTemplate"
                v-model="smsForm.messageCode"
                style="width: 35%"
              >
                <Option
                  v-for="item in smsTemplates"
                  :key="item.id"
                  :value="item.templateCode"
                  >{{ item.templateName }}
                </Option>
              </Select>
            </FormItem>
            <FormItem label="短信内容" prop="context">
              <Input
                v-model="smsForm.context"
                clearable
                type="textarea"
                style="width: 90%"
                maxlength="50"
                :autosize="{ maxRows: 4, minRows: 4 }"
                show-word-limit
                disabled
              />
            </FormItem>
            <FormItem label="接收人" prop="smsRange">
              <p>
                已选<span style="color: #f56c1d"> {{ memberNum }}</span
                >人，预计耗费条数<span style="color: #f56c1d"
                  >{{ memberNum }}条</span
                >
              </p>
              <RadioGroup
                type="button"
                button-style="solid"
                @on-change="smsRangeChange"
                v-model="smsForm.allSendMark"
              >
                <Radio label="1">全部发送</Radio>
                <Radio label="2">条件发送</Radio>
              </RadioGroup>
            </FormItem>
            <FormItem>
              <div style="width: 700px" v-if="customSms">
                <Tabs value="B端合伙人" @on-click="switchClick">
                  <TabPane label="B端合伙人" name="B端合伙人">
                    <bdPartner
                      ref="bdPartner"
                      :flag="sendSmsModal"
                      @change="changeModal"
                      @changeNum="changeNum"
                      @list="listNum"
                    ></bdPartner>
                  </TabPane>
                  <TabPane label="天使合伙人" name="天使合伙人">
                    <tianshiPartner
                      ref="tianshiPartner"
                      :flag="sendSmsModal"
                      @change="changeModal"
                      @changeNum="changeNum"
                      @list="listNum"
                    ></tianshiPartner>
                  </TabPane>
                  <TabPane label="事业合伙人" name="事业合伙人">
                    <shiyePartner
                      ref="shiyePartner"
                      :flag="sendSmsModal"
                      @change="changeModal"
                      @changeNum="changeNum"
                      @list="listNum"
                    ></shiyePartner>
                  </TabPane>
                  <TabPane label="城市合伙人" name="城市合伙人">
                    <shiyePartner
                      ref="chengshiPartner"
                      :flag="sendSmsModal"
                      @change="changeModal"
                      @changeNum="changeNum"
                      @list="listNum"
                    ></shiyePartner>
                  </TabPane>
                  <TabPane label="C端会员" name="C端会员">
                    <cdPartner
                      ref="cdPartner"
                      :flag="sendSmsModal"
                      @change="changeModal"
                      @changeNum="changeNum"
                      @list="listNum"
                    ></cdPartner>
                  </TabPane>
                </Tabs>
              </div>
            </FormItem>
          </Form>
        </div>
      </div>
      <div slot="footer">
        <!-- @click="sendSmsModal = false" -->
        <Button type="text" @click="cancel">取消</Button>
        <Button type="primary" :loading="submitLoading" @click="sendSms"
          >发送</Button
        >
      </div>
    </Modal>
  </div>
</template>


<script>
import {
  getSmsTemplate,
  getSmsSign,
  getMemberAllCount,
  sendAllRecords,
  getsendRecordCout,
} from "@/api/sms.js";
import * as API_Setting from "@/api/setting.js";
import * as API_Member from "@/api/member.js";
import regionArea from "@/views/agent-manage/region";
import bdPartner from "@/views/sys/message/category/bdPartner";
import tianshiPartner from "@/views/sys/message/category/tianshiPartner";
import shiyePartner from "@/views/sys/message/category/shiyePartner";
import chengshiPartner from "@/views/sys/message/category/chengshiPartner";
import cdPartner from "@/views/sys/message/category/cdPartner";
export default {
  name: "sms",
  components: {
    regionArea,
    bdPartner,
    tianshiPartner,
    shiyePartner,
    chengshiPartner,
    cdPartner,
  },
  data() {
    return {
      selectDate: null,
      statusForm: {
        startDate: "",
        endDate: "",
      },
      bdSearchForm: {
        mobile: "",
        nickName: "",
        level: "",
        storeName: "",
        roleType: "",
      },
      bdcolumns: [
        {
          type: "selection",
          width: 60,
          align: "center",
        },
        {
          title: "姓名",
          key: "name",
        },
        {
          title: "店铺名称",
          key: "age",
        },
        {
          title: "类型",
          key: "address",
        },
        {
          title: "电话",
          key: "date",
        },
        // {
        //   title: '地区',
        //   key: 'date'
        // }
      ],
      data1: [
        {
          name: "John Brown",
          age: 18,
          address: "New York No. 1 Lake Park",
          date: "2016-10-03",
        },
        {
          name: "Jim Green",
          age: 24,
          address: "London No. 1 Lake Park",
          date: "2016-10-01",
        },
        {
          name: "Joe Black",
          age: 30,
          address: "Sydney No. 1 Lake Park",
          date: "2016-10-02",
        },
        {
          name: "Jon Snow",
          age: 26,
          address: "Ottawa No. 2 Lake Park",
          date: "2016-10-04",
        },
      ],
      sendRecordTitle: "状态记录",
      sendRecordModal: false, //状态记录框
      successCount: 0, //成功条数
      failCount: 0, //失败条数
      loading: false,
      customSms: false, //当选择自动发送对象 展示
      alreadyCheck: [], //已经选中的数据
      alreadyCheckShow: [], //已经选择的值负责显示
      memberPage: 0, // 会员信息
      members: [], //所有会员
      smsTemplateContent: "", //短信模板内容
      memberNum: 0, //会员总数
      smsContent: "<div class='sms'>效果预览</div>", //短信内容
      smsTemplates: [], //短信模板
      smsSigns: [], //短信签名
      sendSmsModal: false, //弹出发送短信模态框
      sendSmsModalTitle: "短信发送", //发送短信模态框标题
      modalType: 0, // 添加或编辑标识
      templateModalVisible: false, //添加短信模板弹出框
      templateModalTitle: "", //添加短信模板弹出框标题
      templateForm: {}, //短信模板添加form
      submitLoading: false, // 提交加载状态
      signSearchForm: {
        // 搜索框初始化对象
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
      },
      //短信模板查询form
      templateSearchForm: {
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
      },
      //会员条数查询form
      memberSearchFrom: {
        disabled: "OPEN", // 会员状态
      },
      //会员查询条件
      memberSearchParam: {
        pageNumber: 1, // 当前页数
        pageSize: 8, // 页面大小
        disabled: "OPEN", // 会员状态
      },
      //短信记录查询form
      smsSearchForm: {
        sort: "createTime",
        order: "desc",
        pageNumber: 1, // 当前页数
        pageSize: 10, // 页面大小
      },
      smsForm: {
        memberIds: "",
        phones: "",
        num: "0",
        signName: "",
        context: "",
        messageCode: "",
        verificationEnums: "SMS_BATCH",
        allSendMark: "1",
      },
      //短信发送参数校验
      smsFormValidate: {
        signName: [
          {
            required: true,
            message: "请选择短信签名",
            trigger: "change",
          },
        ],
        messageCode: [
          {
            required: true,
            message: "请选择短信模板",
            trigger: "change",
          },
        ],
      },
      templateFormValidate: {
        templateName: [
          {
            required: true,
            message: "请输入短信模板名称",
            trigger: "blur",
          },
        ],
        templateContent: [
          {
            required: true,
            message: "请输入短信模板内容",
            trigger: "blur",
          },
        ],
        remark: [
          {
            required: true,
            message: "请输入短信模板申请说明",
            trigger: "blur",
          },
        ],
      },
      smsColumns: [
        {
          title: "短信签名",
          width: 150,
          key: "signName",
        },
        {
          title: "短信模板",
          minWidth: 200,
          key: "context",
        },
        {
          title: "操作时间",
          width: 150,
          key: "createTime",
        },
        {
          title: "已选人数",
          key: "num",
          width: 140,
        },
        {
          title: "状态",
          key: "sendType",
          width: 140,
          render: (h, params) => {
            if (params.row.sendType == 1) {
              return h("div", {}, "已发送");
            }
          },
        },
      ],
      smsData: [], // 表单数据
      smsTotal: 0, // 表单数据总数
      templateColumns: [
        {
          title: "模板code",
          key: "templateCode",
        },
        {
          title: "模板名称",
          key: "templateName",
        },
        {
          title: "模板内容",
          key: "templateContent",
        },
        {
          title: "状态",
          key: "templateStatus",
          headerAlign: "center",
          Width: "100px",
          render: (h, params) => {
            if (params.row.templateStatus == 0) {
              return h("div", {}, "审核中");
            } else if (params.row.templateStatus == 1) {
              return h("div", {}, "审核通过");
            } else {
              return h("div", {}, "审核失败");
            }
          },
        },
        {
          title: "操作",
          key: "action",
          fixed: "right",
          width: 200,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "info",
                    size: "small",
                  },
                  style: {
                    marginRight: "5px",
                  },
                  attrs: {
                    disabled: params.row.templateStatus == 2 ? false : true,
                  },
                  on: {
                    click: () => {
                      this.editTemplate(params.row);
                    },
                  },
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small",
                  },
                  style: {
                    marginRight: "5px",
                  },
                  attrs: {
                    disabled: params.row.templateStatus == 0 ? true : false,
                  },
                  on: {
                    click: () => {
                      this.deleteSmsTemplate(params.row);
                    },
                  },
                },
                "删除"
              ),
            ]);
          },
        },
      ],
      templateData: [], // 表单数据
      templateTotal: 0, // 表单数据总数
      signColumns: [
        {
          title: "签名名称",
          key: "signName",
        },
        {
          title: "申请说明",
          key: "remark",
        },
        {
          title: "状态",
          key: "signStatus",
          headerAlign: "center",
          Width: "100px",
          slot: "signStatus",
        },
        {
          title: "操作",
          key: "action",
          fixed: "right",
          width: 200,
          align: "center",
          render: (h, params) => {
            return h("div", [
              h(
                "Button",
                {
                  props: {
                    type: "info",
                    size: "small",
                    icon: "ios-create-outline",
                  },
                  attrs: {
                    disabled: params.row.signStatus == 2 ? false : true,
                  },
                  style: {
                    marginRight: "5px",
                  },
                  on: {
                    click: () => {
                      this.editSign(params.row);
                    },
                  },
                },
                "编辑"
              ),
              h(
                "Button",
                {
                  props: {
                    type: "error",
                    size: "small",
                    icon: "ios-create-outline",
                  },
                  attrs: {
                    disabled: params.row.signStatus == 0 ? true : false,
                  },
                  style: {
                    marginRight: "5px",
                  },
                  on: {
                    click: () => {
                      this.deleteSmsSign(params.row);
                    },
                  },
                },
                "删除"
              ),
            ]);
          },
        },
      ],
      signData: [], // 表单数据
      signTotal: 0, // 表单数据总数
    };
  },
  methods: {
    // 搜索关闭按钮
    sendRecordClose() {
      this.statusForm = {
        startDate: "",
        endDate: "",
      };
      this.selectDate = null;
    },
    //搜索
    async statusRecordSearch() {
      if (this.selectDate == null || this.selectDate[0] == "") {
        this.$Message.info("请选择时间");
      } else {
        const res = await getsendRecordCout(this.statusForm);
        if (res.code != 200) return;
        this.successCount = res.respondedSuccessCount;
        this.failCount = res.respondedFailCount;
      }
    },
    selectDateRange(val) {
      if (val) {
        this.statusForm.startDate = val[0].replace(/[-]/g, "");
        this.statusForm.endDate = val[1].replace(/[-]/g, "");
      }
    },
    statusRecord() {
      this.sendRecordModal = true;
    },
    // 关闭
    close() {
      this.smsForm = {
        memberIds: "",
        phones: "",
        num: "",
        signName: "",
        context: "",
        messageCode: "",
        verificationEnums: "SMS_BATCH",
      };
      this.smsRangeChange(1);
    },
    // 取消
    cancel() {
      this.sendSmsModal = false;
      this.smsForm = {
        memberIds: "",
        phones: "",
        num: "",
        signName: "",
        context: "",
        messageCode: "",
        verificationEnums: "SMS_BATCH",
      };
      this.smsRangeChange(1);
    },
    listNum(val) {
      this.smsForm.memberIds = "";
      this.smsForm.phones = "";
      // this.smsForm.memberIds=JSON.stringify(val.map((item)=>{
      //   return item.id
      // }))
      this.smsForm.phones += val.map((item) => {
        return item.mobile;
      });
      this.smsForm.memberIds += val.map((item) => {
        return item.id;
      });
    },
    changeNum(val) {
      this.memberNum = val;
      this.smsForm.num = this.memberNum;
    },
    changeModal(val) {
      this.sendSmsModal = val;
    },
    // 获取短信模板
    async getsms(params) {
      const res = await getSmsTemplate(params);
      if (res.code != 200) return;
      this.smsTemplates = res.result.records;
    },
    //获取会员人数
    async getCount() {
      const res = await getMemberAllCount();
      if (res.code != 200) return;
      this.memberNum = res.count;
    },
    //获取短信签名
    async getSmsSignAPI(params) {
      const res = await getSmsSign(params);
      if (res.code != 200) return;
      this.smsSigns = res.result;
    },
    // 类型切换
    switchClick(val) {
      this.memberNum = 0;
      this.$refs.bdPartner.data1 = [];
      this.$refs.tianshiPartner.data = [];
      this.$refs.shiyePartner.data = [];
      this.$refs.chengshiPartner.data = [];
      this.$refs.cdPartner.data = [];
      this.$refs.bdPartner.bdSearchForm = {
        pageNumber: 1,
        pageSize: 9999999,
        mobile: "",
        nickName: "",
        storeName: "",
        roleType: "STORE",
        regionId: "",
      };
      this.$refs.tianshiPartner.bdSearchForm = {
        pageNumber: 1,
        pageSize: 9999999,
        mobile: "",
        nickName: "",
        roleType: "",
        regionId: "",
        level: "",
      };
      this.$refs.shiyePartner.bdSearchForm = {
        pageNumber: 1,
        pageSize: 9999999,
        mobile: "",
        nickName: "",
        roleType: "",
        regionId: "",
        level: "",
      };
      this.$refs.chengshiPartner.bdSearchForm = {
        pageNumber: 1,
        pageSize: 9999999,
        mobile: "",
        nickName: "",
        roleType: "",
        regionId: "",
        level: "",
      };
      this.$refs.cdPartner.bdSearchForm = {
        pageNumber: 1,
        pageSize: 9999999,
        mobile: "",
        roleType: "",
        regionId: "",
        level: "",
      };
      if (val == "B端合伙人") {
        this.$refs.bdPartner.bdSearchForm.roleType = "STORE";
        this.$refs.bdPartner.initTableList();
      } else if (val == "天使合伙人") {
        this.$refs.tianshiPartner.bdSearchForm.roleType = "ANGEL_PARTNER";
        this.$refs.tianshiPartner.initTableList();
      } else if (val == "事业合伙人") {
        this.$refs.shiyePartner.bdSearchForm.roleType = "BUSINESS_PARTNER";
        this.$refs.shiyePartner.initTableList();
      } else if (val == "城市合伙人") {
        this.$refs.chengshiPartner.bdSearchForm.roleType = "AGENT";
        this.$refs.chengshiPartner.initTableList();
      } else {
        this.$refs.cdPartner.bdSearchForm.roleType = "C_MEMBER";
        this.$refs.cdPartner.initTableList();
      }
    },
    //选择接收人事件
    smsRangeChange(v) {
      this.customSms = true;
      this.memberNum = 0;
      if (v == 1) {
        // this.cancelSend=true
        this.alreadyCheck = [];
        this.alreadyCheckShow = [];
        this.customSms = false;
        this.getCount();
        this.smsForm.allSendMark = "1";
      }
      if (v == 2) {
        this.smsForm.allSendMark = "2";
        this.customSms = true;
        // this.cancelSend=false;
        if (this.changeNum() == undefined) {
          this.memberNum = 0;
        } else {
          this.changeNum();
        }
      }
    },
    //弹出发送短信模态框
    sendBatchSmsModal() {
      this.getsms("0");
      this.getSmsSignAPI("0");
      this.getCount();
      this.loading = false;
      this.sendSmsModal = true;
    },
    //pane切换事件
    paneChange(v) {
      if (v == "TEMPLATE") {
        this.getSmsTemplate();
      }
      if (v == "SIGN") {
        this.getSmsSign();
      }
    },
    //短信签名变化方法
    selectSmsSign(v) {
      console.log(v);
      if (v != void 0) {
        //给预览赋值
        this.smsContent =
          "<div class='sms'>【" +
          v +
          "】" +
          " " +
          this.smsTemplateContent +
          "</div>";
      } else {
        this.smsContent =
          "<div class='sms'>效果预览" + this.smsTemplateContent + "</div>";
      }
    },
    //短信模板变化方法
    selectSmsTemplate(v) {
      if (!v) {
        this.smsForm.context = "";
        return;
      }
      //循环短信模板 如果选择短信模板匹配则查询出当前模板的内容
      this.smsTemplates.forEach((e) => {
        if (this.smsForm.messageCode == e.templateCode) {
          this.smsTemplateContent = e.templateContent;
          // this.smsForm.smsName = e.templateName;
        }
      });
      if (this.smsForm.signName != "" && this.smsForm.signName != void 0) {
        this.smsContent =
          "<div class='sms'>【" +
          this.smsForm.signName +
          "】" +
          this.smsTemplateContent +
          "</div>";
      } else {
        this.smsContent =
          "<div class='sms'>" + this.smsTemplateContent + "</div>";
      }
      this.smsForm.context = this.smsTemplateContent;
    },
    //删除短信模板
    deleteSmsTemplate(v) {
      let params = {
        templateCode: v.templateCode,
      };
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除此短信模板？",
        loading: true,
        onOk: () => {
          API_Setting.deleteSmsTemplatePage(params).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("删除成功");
              this.getSmsTemplate();
            }
          });
        },
      });
    },
    //发送短信
    sendSms() {
      this.$refs.smsForm.validate((valid) => {
        if (valid) {
          if (this.memberNum == 0) {
            this.$Message.info("请选择要发送短信人员");
          } else {
            sendAllRecords(this.smsForm).then((res) => {
              if (res.success) {
                this.$Message.success("发送成功");
                this.sendSmsModal = false;
                this.smsRangeChange(1);
                this.getSms();
                this.smsForm = {
                  memberIds: "",
                  phones: "",
                  num: "",
                  signName: "",
                  context: "",
                  messageCode: "",
                  verificationEnums: "SMS_BATCH",
                  allSendMark: "1",
                };
              }
            });
          }
        }
      });
    },
    //添加短信签名
    addSign() {
      this.$router.push({ name: "add-sms-sign" });
    },
    //新增短信模板
    addTemplate() {
      this.templateModalVisible = true;
      this.templateModalTitle = "添加短信模板";
      this.templateForm = {};
    },
    //修改短信模板
    editTemplate(v) {
      this.templateModalVisible = true;
      this.templateModalTitle = "修改短信模板";
      this.templateForm = v;
      this.modalType = 1;
    },
    //同步签名
    syncSign() {
      this.loading = true;
      API_Setting.syncSign().then((res) => {
        this.loading = false;
        if (res.success) {
          this.$Message.success("同步成功");
          this.getSmsSign();
        }
      });
      this.loading = false;
    },
    //短信模板添加提交
    templateSubmit() {
      this.$refs.templateForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.modalType == 0) {
            API_Setting.addSmsTemplatePage(this.templateForm)
              .then((res) => {
                this.loading = false;
                if (res.success) {
                  this.$Message.success("添加成功");
                  this.loading = false;
                  this.templateModalVisible = false;
                  this.getSmsTemplate();
                }
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            API_Setting.editSmsTemplatePage(this.templateForm)
              .then((res) => {
                this.loading = false;
                if (res.success) {
                  this.$Message.success("修改成功");
                  this.loading = false;
                  this.templateModalVisible = false;
                  this.getSmsTemplate();
                }
              })
              .catch(() => {
                this.loading = false;
              });
          }
        }
      });
    },
    //删除短信签名
    deleteSmsSign(v) {
      this.$Modal.confirm({
        title: "确认删除",
        content: "您确认要删除此短信签名？",
        loading: true,
        onOk: () => {
          API_Setting.deleteSign(v.id).then((res) => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success("删除成功");
              this.getSmsTemplate();
            }
          });
        },
      });
    },
    //短信模板同步
    syncTemplate() {
      this.loading = true;
      API_Setting.syncTemplate().then((res) => {
        this.loading = false;
        if (res.success) {
          this.$Message.success("同步成功");
          this.getSmsTemplate();
        }
      });
      this.loading = false;
    },
    //短信记录页数变化
    smsChangePage(v) {
      this.smsSearchForm.pageNumber = v;
      this.getSms();
    },
    //短信记录页数变化
    smsChangePageSize(v) {
      this.smsSearchForm.pageNumber = 1;
      this.smsSearchForm.pageSize = v;
      this.getSms();
    },
    //短信模板页数变化
    templateChangePage(v) {
      this.templateSearchForm.pageNumber = v;
      this.getSmsTemplate();
    },
    //短信模板页数变化
    templateChangePageSize(v) {
      this.templateSearchForm.pageNumber = 1;
      this.templateSearchForm.pageSize = v;
      this.getSmsTemplate();
    },
    //分页获取短信模板数据
    getSmsTemplate() {
      this.loading = true;
      API_Setting.getSmsTemplatePage(this.templateSearchForm).then((res) => {
        this.loading = false;
        if (res.success) {
          this.templateData = res.result.records;
          this.templateTotal = res.result.total;
        }
      });
      this.loading = false;
    },
    // 分页获取短信记录数据
    getSms() {
      this.loading = true;
      API_Setting.getSmsPage(this.smsSearchForm).then((res) => {
        this.loading = false;
        if (res.success) {
          this.smsData = res.result.records;
          this.smsTotal = res.result.total;
        }
      });
      this.loading = false;
    },
    //短信模板页数变化
    signChangePage(v) {
      this.signSearchForm.pageNumber = v;
      this.getSmsSign();
    },
    //短信模板页数变化
    signChangePageSize(v) {
      this.signSearchForm.pageNumber = 1;
      this.signSearchForm.pageSize = v;
      this.getSmsSign();
    },
    //修改短信签名
    editSign(v) {
      this.$router.push({ name: "add-sms-sign", query: { id: v.id } });
    },
    //分页获取短信签名数据
    getSmsSign() {
      this.loading = true;
      API_Setting.getSmsSignPage(this.signSearchForm).then((res) => {
        this.loading = false;
        if (res.success) {
          this.signData = res.result.records;
          this.signTotal = res.result.total;
        }
      });
      this.loading = false;
    },
  },
  mounted() {
    this.getSms();
  },
};
</script>

<style lang="scss">
@import "sms.scss";

.split {
  height: 200px;
  border: 1px solid #dcdee2;
}

.split-pane {
  padding: 10px;
}

.ivu-select-selection {
  width: 148px;
}

.search-form {
  margin-bottom: 10px;
}
</style>
